import { getUserInfo, editUserInfo, getRealInfo } from "../api/Interface/user";
import { useLoginStore } from "../store/modules/login";
import { useUserStore } from "../store/modules/user";
import BasicConfig from "../utils/config";
import errorCode from "../utils/http/errorCode";
import { useSetUserStorage } from "./useStorage";

/** 获取用户信息 */
export const useGetUserInfo = async () => {
  const res = await getUserInfo().catch((err) => {
    console.log("获取用户信息请求err");
    return Promise.reject(err);
  });
  if (res.code === errorCode.success) {
    console.log("获取用户信息请求res");
    const Store = useUserStore();
    Store.setUserInfo(res.data);
    // useSetUserStorage(res.data);
  }
  // else BasicConfig.toast(res.msg);
  return Promise.resolve(res);
};

/** 修改用户信息 */
export const useEditUserInfo = async (params) => {
  const res = await editUserInfo(params).catch((err) => {
    return Promise.reject(err);
  });
  if (res.code === errorCode.success) await useGetUserInfo();
  return Promise.resolve(res);
};

/**
 * 获取用户实名认证信息
 * showModal: 是否显示模态框
 */
export const useGetRealInfo = async (showModal = true) => {
  const { code, data } = await getRealInfo();
  if (code === errorCode.noReal && showModal) {
    uni.showModal({
      content: "您还未实名认证，请先实名认证后再进行后续操作",
      showCancel: true,
      confirmText: "去认证",
      confirmColor: BasicConfig.MainColor,
      success: ({ confirm }) => {
        if (confirm) {
          uni.navigateTo({
            url: "/package-my/pages/real/real",
          });
        }
      },
    });
  }
  return Promise.resolve(code === errorCode.noReal ? null : data);
};
